package com.ruoyi.web.controller.system;

/**
 * @author zxz
 * @date 2025/5/14
 * @description
 */

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IExpertScoringService;
import com.ruoyi.system.service.ITenderItemService;
import com.ruoyi.system.service.impl.ExpertScoringServiceImpl;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import static com.ruoyi.common.utils.file.FileUploadUtils.getAbsoluteFile;

@Controller
@RequestMapping("/system/download")
public class DownloadController {
    private String prefix = "system/download";

    @Autowired
    private ITenderItemService tenderItemService;
    @Autowired
    private IExpertScoringService expertScoringService;

    @GetMapping()
    public String download() throws IOException {
        return prefix + "/download";
    }

    /**
     * 修改项目管理
     */
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult edit(String ids,HttpServletResponse response) throws IOException {
        TenderItem tenderItem = tenderItemService.selectTenderItemByItemId(Long.parseLong(ids));
        // 单个字段填充，使用map格式
        Map<String, Object> otherData = new HashMap<>();
        otherData.put("itemName", tenderItem.getItemName());
        otherData.put("itemId", tenderItem.getItemCode());
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 创建中文日期格式器
        DateTimeFormatter chineseFormatter = DateTimeFormatter
                .ofPattern("yyyy年M月d日", Locale.CHINESE);
        otherData.put("time", currentDate.format(chineseFormatter));
        // 输出文件路径
        String filename = ExcelUtil.encodingFilename("ceshi");
        String absoluteFile = ExcelUtil.getAbsoluteFile(filename);
        // 输出文件路径
        URL resource = getClass().getClassLoader().getResource("template.xlsx");
        String path = resource.getPath();
        //对象封装列表数据

        List<ExpertScoring> expertScorings = expertScoringService.selectByItemId(tenderItem.getItemId());
        Long userId = expertScorings.get(0).getUserId();
        int i = 0;
        for (ExpertScoring expertScoring : expertScorings) {
            if (userId == expertScoring.getUserId()) {
                i++;
            }
        }
        List<ExeDown> exeDowns = new ArrayList<>();
        for (int j = 0; j < i; j++) {
            ExpertScoring expertScoring = expertScorings.get(j);
            ExeDown exeDown = new ExeDown();
            exeDown.setId(j+1);
            exeDown.setSbName("测试设备名称");
            String firstLevelCategory = expertScoring.getFirstLevelCategory();
            String expertFirst = "";
            if ("1".equals(firstLevelCategory)) {
                expertFirst = "资格审查";
            }else if ("2".equals(firstLevelCategory)) {
                expertFirst = "符合性审查";
            }else if ("3".equals(firstLevelCategory)) {
                expertFirst = "商务";
            }else if ("4".equals(firstLevelCategory)) {
                expertFirst = "技术";
            }else if ("5".equals(firstLevelCategory)) {
                expertFirst = "价格";
            }else {
                expertFirst = "其他";
            }
            exeDown.setFirstLevelCategory(expertFirst);
            exeDown.setSecondLevelCategory(expertScoring.getSecondLevelCategory());
            exeDown.setItemBz(expertScoring.getItemBz());
            exeDown.setBzf("100");
            exeDown.setScoreSet(expertScoring.getScoreSet());
            exeDowns.add(exeDown);
        }
        // 1、直接通过文件模板填充
        try (ExcelWriter excelWriter = EasyExcel.write(absoluteFile).withTemplate(path).build();) {
            // 创建写入工作表
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            // 每次使用 list 参数时创建新行。
            FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();
            // 针对某个字段填充
            excelWriter.fill(otherData, fillConfig, writeSheet);
            excelWriter.fill(exeDowns, fillConfig, writeSheet);
        }

        return AjaxResult.success(filename);
    }


}
